package com.yt.util;

import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * Created by liujunshi on 16-1-22.
 */
public class DateUtils {
	private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);
	private static final String MON_FMT = "yyyy-MM";
	private static final String DAY_FMT = "yyyy-MM-dd";
	private static final String DAYTIME_FMT = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 获取对应的毫秒区间(前闭后开)
	 * @param year 指定年
	 * @param month 指定月
	 * @return 单位（毫秒）
	 */
	public static long[] getMonthRangeInMillis(int year, int month) {
		Preconditions.checkArgument(year > 0, "Invalid year argument: %s", year);
		Preconditions.checkArgument(month >= 1 & month <= 12, "Invalid month argument: %s", month);

		long[] longPair = new long[2];

		DateFormat formatter = new SimpleDateFormat("yyyy-MM");

		try {
			String beginOfMonth = year + "-" + month;
			Date date = formatter.parse(beginOfMonth);
			longPair[0] = date.getTime();

			String beginOfNextMonth = (month == 12 ? (year + 1) + "-1" : year + "-" + (month + 1));
			date = formatter.parse(beginOfNextMonth);
			longPair[1] = date.getTime();
		} catch (ParseException pe) {
			throw new RuntimeException("SimpleDateFormat将字符串转换成日期异常: " + year + "_" + month, pe.getCause());
		}

		return longPair;
	}

	/**
	 * 获取对应的毫秒区间(前闭后开)
	 * @param year 指定年
	 * @param month 指定月
	 * @return 单位（秒）
	 */
	public static long[] getMonthRangeInSeconds(int year, int month) {
		long[] longPair = getMonthRangeInMillis(year, month);
		longPair[0] = longPair[0] / 1000;
		longPair[1] = longPair[1] / 1000;
		return longPair;
	}

	/**
	 * 获取上月
	 * @return yyyy-MM-dd
	 */
	public static String getLastMonStr() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONDAY, -1);
		return getMonthStr(cal.getTimeInMillis());
	}

	/**
	 * 获取上上月
	 * @return yyyy-MM-dd
	 */
	public static String getLast2MonStr() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONDAY, -2);
		return getMonthStr(cal.getTimeInMillis());
	}

	public static String getMonthStr(long millis) {
		SimpleDateFormat fmt = new SimpleDateFormat(MON_FMT);
		return fmt.format(new Date(millis));
	}

	public static String getMonthStr() {
		SimpleDateFormat fmt = new SimpleDateFormat(MON_FMT);
		return fmt.format(new Date());
	}
	public static String formatDate(Date date) {
		SimpleDateFormat fmt = new SimpleDateFormat(DAY_FMT);
		return fmt.format(date);
	}
	public static String formatDateTime(Date date) {
		SimpleDateFormat fmt = new SimpleDateFormat(DAYTIME_FMT);
		return fmt.format(date);
	}
	public static Date parseDate(String dateStr) {
		SimpleDateFormat fmt = new SimpleDateFormat(DAY_FMT);
		try{
			return fmt.parse(dateStr);
		}catch (Exception ex){
			return null;
		}
	}
	public static Date parseDateTime(String dateStr) {
		SimpleDateFormat fmt = new SimpleDateFormat(DAYTIME_FMT);
		try{
			return fmt.parse(dateStr);
		}catch (Exception ex){
			return null;
		}
	}
	/**
	 * **返回PHP风格的dateline
	 * @return
	 */
	public static long getDateline() {
		return System.currentTimeMillis() / 1000;
	}

	public static String getDayStr() {
		SimpleDateFormat fmt = new SimpleDateFormat(DAY_FMT);
		return fmt.format(new Date());
	}

	public static String getDayStr(long millis) {
		SimpleDateFormat fmt = new SimpleDateFormat(DAY_FMT);
		return fmt.format(new Date(millis));
	}

	public static int getIntervalOfDays(String fromDate, String toDate) {
		SimpleDateFormat fmt = new SimpleDateFormat(DAY_FMT);
		int interval = 0;;
		try {
			interval = getIntervalOfDays(fmt.parse(fromDate), fmt.parse(toDate));
		} catch (ParseException e) {
			logger.error("转行日期字符串异常：fromDate[{}], toDate[{}]", fromDate, toDate);
			e.printStackTrace();
		}

		return interval;
	}

	public static int getIntervalOfDays(Date fromDate, Date toDate) {
		long diffInMillis = toDate.getTime() - fromDate.getTime();
		return (int) (diffInMillis / TimeUnit.DAYS.toMillis(1));
	}

	public static Long getTodaysFirstSecond(Long dateline) {
		return (dateline + 28800) / 86400 * 86400 - 28800;
	}
}
